package com.enqualcomm.kids.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;

import com.enqualcomm.kids.R;

/**
 * 
 * 
 * @author yzq
 *
 */
public class ChartLayout extends LinearLayout {
    private OnTouchChartItemListener listener;
	private ChartItem preSelectedItem;
	private int progressBackground1;
	private int progressBackground2;
	private int itemBg;
	private int descColor1;
	private int descColor2;
	
	public ChartLayout(Context context, AttributeSet attrs) {
		super(context, attrs);

        String namespace = "http://schemas.android.com/apk/res-auto";
        int itemCount = attrs.getAttributeIntValue(namespace, "chartItemCount", 0);
		boolean secondProgress = attrs.getAttributeBooleanValue(namespace, "secondProgress", false);
		if (itemCount>0) {
			TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ChartLayout);
			float padding = typedArray.getDimension(R.styleable.ChartLayout_chartItemPadding, 20);
			float descHeight = typedArray.getDimension(R.styleable.ChartLayout_descHeight, 0);
			progressBackground1 = typedArray.getResourceId(R.styleable.ChartLayout_progressBackground1,
						R.drawable.selector_chart_progress1);
			progressBackground2 = typedArray.getResourceId(R.styleable.ChartLayout_progressBackground2,
					R.drawable.selector_chart_progress1);
			itemBg = typedArray.getResourceId(R.styleable.ChartLayout_itemBg,-1);
			
			
			descColor1 = typedArray.getResourceId(R.styleable.ChartLayout_descColor1,-1);
			descColor2 = typedArray.getResourceId(R.styleable.ChartLayout_descColor2,-1);
			
			setOrientation(HORIZONTAL);
			initItem(context, itemCount, padding, descHeight, secondProgress);

            typedArray.recycle();
		}
//		System.out.print("itemchout:" + itemCount);
	}
	
	private void initItem(Context context, int itemCount,float padding,float descHeight,boolean secondProgress) {
		LayoutParams params = new LayoutParams(0,LayoutParams.MATCH_PARENT, 1);
		ChartItem item = null;
		for (int i = 0; i < itemCount; i++) {
			item = new ChartItem(context,padding,descHeight,secondProgress);
			if (itemBg!=-1) {
				item.setBackgroundResource(itemBg);
			}
			addView(item, params);
		}
	}
	/**
	 * 
	 * @param max
	 * 最大值
	 * @param base
	 * 进度条默认使用背景progressBackground1，少于base使用progressBackground2
	 * @param progress
	 * 当前进度
	 * @param desc
	 */
	public void setChartValues(int max,int base,int[] progress,String[] desc){
		int j = getChildCount()>progress.length?progress.length:getChildCount();
		ChartItem item;
		for (int i = 0; i <j; i++) {
			item = (ChartItem)getChildAt(i);
			item.setMax(max);
			item.setProgress(progress[i]);
			
			if (progress[i]<base) {
				item.setProgressBackground(progressBackground2);
				if (descColor2!=-1) {
					item.setDescColor(descColor2);
				}
			}else {
				item.setProgressBackground(progressBackground1);
				if (descColor1!=-1) {
					item.setDescColor(descColor1);
				}
			}
			if (desc!=null&&desc.length>i) {
				item.setDesc(desc[i]);
			}
		}
	}
	
/*	public void selecteChartItem(int index){
		ChartItem view = (ChartItem) getChildAt(index);
		if (view!=null) {
			view.setSelected(true);
			preSelectedItem = view;
//			if (listener!=null) {
//				listener.onTouch(view);
//			}
		}
	}*/
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
//		System.out.println("dispatchTouchEvent!!!");
		
		switch (ev.getAction()) {
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL://手指抬起的时候，把选中的都清除
			if (preSelectedItem!=null) {
				preSelectedItem.setSelected(false);
				preSelectedItem = null;
				if (listener!=null) {
					listener.onTouch(null,-1);
				}
			}
			break;
		case MotionEvent.ACTION_DOWN://手指落下的时候，把父控件的事件拦截下来
			getParent().getParent().getParent().requestDisallowInterceptTouchEvent(true);
//			break;
		case MotionEvent.ACTION_MOVE:
			Rect r = new Rect();
			ChartItem child;
			for (int i = 0; i < getChildCount(); i++) {
				child = (ChartItem)getChildAt(i);
				if (child.getProgress()==0) {
					continue;
				}
				
				child.getGlobalVisibleRect(r);
				if (r.contains((int)ev.getRawX(), (int)ev.getRawY())) {
					if (preSelectedItem!=null) {
						preSelectedItem.setSelected(false);
					}
					child.setSelected(true);
					preSelectedItem = child;
					if (listener!=null) {
						listener.onTouch(child,i);
					}
					break;
				}
			}
			
			break;
		}
		
		return super.dispatchTouchEvent(ev);
	}
	
	public void setOnTouchChartItemListener(OnTouchChartItemListener listener){
		this.listener = listener;
	}
	public static interface OnTouchChartItemListener{
		void onTouch(ChartItem item, int index);
	}

}
